package org.boticelli;

import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;

import java.util.Arrays;
import java.util.List;

import org.boticelli.util.Util;
import org.junit.Test;

public class UtilTestCase
{
    @Test
    public void thatSplitWorks()
    {
        List<String> list=Util.split("a,b", ",");

        assertThat(list.size(), is(2));
        assertThat(list.get(0), is("a"));
        assertThat(list.get(1), is("b"));

        list=Util.split("ab", ",");
        assertThat(list.size(), is(1));
        assertThat(list.get(0), is("ab"));

        list=Util.split("a,", ",");
        assertThat(list.size(), is(1));
        assertThat(list.get(0), is("a"));
    }

    @Test
    public void thatJoinWorks()
    {
        String s=Util.join(Arrays.asList("foo","bar"), " ");

        assertThat(s,is("foo bar"));
    }

    @Test
    public void testQuoted()
    {
        List<String> list=Util.splitWords("a bb ccc");
        assertThat(list.size(), is(3));
        assertThat(list.get(0), is("a"));
        assertThat(list.get(1), is("bb"));
        assertThat(list.get(2), is("ccc"));

        list=Util.splitWords("ab");
        assertThat(list.size(), is(1));
        assertThat(list.get(0), is("ab"));

        list=Util.splitWords("a ");
        assertThat(list.size(), is(1));
        assertThat(list.get(0), is("a"));


        list=Util.splitWords("\"a c\" b");
        assertThat(list.size(), is(2));
        assertThat(list.get(0), is("a c"));
        assertThat(list.get(1), is("b"));

        list=Util.splitWords("'a c' b");
        assertThat(list.get(0), is("'a"));
        assertThat(list.get(1), is("c'"));
        assertThat(list.get(2), is("b"));

        list=Util.splitWords("\\\"a c\\\" b");
        assertThat(list.size(), is(3));
        assertThat(list.get(0), is("\"a"));
        assertThat(list.get(1), is("c\""));
        assertThat(list.get(2), is("b"));

        list=Util.splitWords("\\\\\"a c\\\\\" b");
        //System.out.println(list);
        assertThat(list.size(), is(3));
        assertThat(list.get(0), is("\\"));
        assertThat(list.get(1), is("a c\\"));
        assertThat(list.get(2), is("b"));
    }

    @Test
    public void testJoinWords()
    {
        String s=Util.joinWords(Arrays.asList("foo", "bar"));
        assertThat(s, is("foo bar"));

        s=Util.joinWords(Arrays.asList("a\" b", "bar"));
        assertThat(s, is("\"a\\\" b\" bar"));
    }

}
